;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Jelle Licht <jlicht@fsfe.org>
;;;
;;; This file is not part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (staging docker-download)
  #:use-module (guix derivations)
  #:use-module (guix gexp)
  #:use-module (guix git-download)
  #:use-module (guix modules)
  #:use-module (guix monads)
  #:use-module (guix packages)
  #:use-module (guix profiles)
  #:use-module (guix store)
  #:use-module (gnu packages virtualization)
  #:use-module (ice-9 match)
  #:export (docker-fetch))

(define-public skopeo-next
  (package
    (inherit skopeo)
    (name "skopeo")
    (version "1.1.0")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/projectatomic/skopeo")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1cxhwfrp5cjdq56vzy7gmidvm1z02f0rz2r1lv0d9ymnjlsjp9s3"))))))

(define* (docker-fetch url hash-algo hash
                       #:optional name
                       #:key (system (%current-system)) (guile (default-guile))
                       (skopeo skopeo-next))
  "Return a fixed-output derivation that fetches URL, which should use the
`docker://' protocol scheme as understood by skopeo.  The output
is expected to HASH of type HASH-ALGO (a symbol).  Use NAME as the file name,
or a generic name if #f."
  (define modules
    (source-module-closure '((guix build utils))))

  (define (build)
    (with-imported-modules modules
      #~(begin
          (use-modules (guix build utils))
          (let ((skopeo (string-append #+skopeo "/bin/skopeo")))
            (invoke skopeo
                    "--insecure-policy"
                    (string-append "--tmpdir=" (getenv "TMPDIR"))
                    "copy"
                    "--src-tls-verify=false"
                    #+url
                    (string-append "docker-archive://"#$output))))))
  (mlet %store-monad
      ((guile (package->derivation guile system)))
    (gexp->derivation (or name "docker-download") (build)
                      #:script-name "docker-download"
                      #:system system
                      #:hash-algo hash-algo
                      #:hash hash
                      #:guile-for-build guile)))

(define ubuntu-test
  (origin
    (method docker-fetch)
    (uri "docker://ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2")
    (file-name "ubuntu-img.tgz")
    (sha256
     (base32
      "0ccvs2zkz12azjvxwmxcccwc4krbnbjsr9kj521mx8n32z0zd0dn"))))

ubuntu-test

